package 牛客.堆栈队列;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Stack;

public class BM43包含min函数的栈 {
    Deque<Integer> stack=new ArrayDeque<>();

    Stack<Integer> stack1=new Stack<>();
    Stack<Integer> minStack=new Stack<>();
    public void push(int node) {
        stack1.push(node);
        if (minStack.isEmpty() || minStack.peek()>node) {
            minStack.push(node);
        }else {
            minStack.push(minStack.peek());
        }
    }

    public void pop() {
        stack1.pop();
        minStack.pop();
    }

    public int top() {
        return stack1.peek();

    }

    public int min() {
        return minStack.peek();
    }

    public int min1() {
        int min=Integer.MAX_VALUE;
        int size=stack.size();
        int[] tmp=new int[size];
        int i=0;
        while (!stack.isEmpty()) {
            int pop=stack.pop();
            if (min>pop) {
                min=pop;
            }
            tmp[i++]=pop;
        }
        for (int j = size-1; j >=0; j--) {
            stack.push(tmp[j]);
        }
        return min;
//        stack为 Stack<Integer> stack=new Stack<>();
//        Integer[] integers = stack.toArray(new Integer[stack.size()]);
//        Arrays.sort(integers);
//        return integers[0];
    }
}
